var ZYRequestClient = require("../../../../utils/zy-request");
var requestClient;
const app = getApp();
Page({
  data:{
    userId:0,
    showPhoto:false,
    navbarData:{
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '确认用印人', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad(){
    var _this = this;
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo !=''){
      _this.setData({
        userId:userInfo.id,
        showPhoto:true,
        'navbarData.title':'确认用印人',
      });
    } else {
      _this.setData({
        showPhoto:false,
        'navbarData.title':'',
      });
      wx.showModal({
        title:'请授权登录',
        content:'盖章前，请先授权登录，来确认您的身份',
        showCancel:false,
        success(r){
          if(r.confirm){
            wx.navigateBack({
              delta: 1,
            })
          }
        }
      })
    }

    requestClient = new ZYRequestClient();
  },
  // 执行拍照
  takePhoto(e) {
    var _this = this;
    const cameraContext = wx.createCameraContext();
    cameraContext.takePhoto({
      quality: 'low',
      success: (res) => {
        // 上传照片
        requestClient.uploadImage(_this.data.userId,res.tempImagePath,function(resp){
          if(resp.data == null || resp.data.code >1){
            wx.showModal({
              title:'上传失败',
              content:'图片上传失败，请重新拍照上传！',
              showCancel:false,
              success(r){

              }
            })
            return;
          }
          wx.setStorageSync('facePhoto', resp.data)
          wx.showToast({
            title: '拍照完毕',
            mask:true,
            duration:1000
          })
          // 返回上一页
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        });
      }
    })
  },
  error(e) {
    wx.showModal({
      title:'获取相机',
      content:'请授权小程序的相机权限！',
      showCancel:false,
      success(r){
        if(r.confirm){
         wx.openSetting({
           withSubscriptions: true,
           success(res){
            wx.redirectTo({
              url:'../face/index'
            });
           }
         })
        }
      }
    })
  }
})